package com.cn.easy.export


import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.FileInputStream
import java.io.FileOutputStream

class T {
}

fun main() {
    val excelFilePath = "D:\\download\\output.xlsx"
    val imageFilePath = "D:\\file\\image\\20240205\\1754316428878372864.jpg"
    val rowIndex = 0 // 行索引
    val columnIndex = 0 // 列索引
    val imageWidth = 100 // 图片宽度（像素）
    val imageHeight = 100 // 图片高度（像素）

    try {
        val workbook = XSSFWorkbook()
        val fileOut = FileOutputStream(excelFilePath)

        // 创建一个工作表
        val sheet = workbook.createSheet("Sheet1")

        // 读取图片文件
        val inputStream = FileInputStream(imageFilePath)
        val bytes = inputStream.use { it.readBytes() }
        val pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG)

        // 关闭输入流
        inputStream.close()

        // 创建一个绘图对象
        val drawing = sheet.createDrawingPatriarch()

        // 创建一个锚点对象
        val anchor = drawing.createAnchor(0, 0, 0, 0, 5, 4, 6, 5)

        // 插入图片
        val picture = drawing.createPicture(anchor, pictureIdx)

        // 设置图片的宽度和高度
//        picture.resize()

        // 保存工作簿
        workbook.write(fileOut)

        // 关闭文件输出流
        fileOut.close()

        println("图片已成功写入 Excel 文件。")

    } catch (e: Exception) {
        e.printStackTrace()
    }
}